package concole;

import java.io.Serializable;
import java.util.ArrayList;

import remoteService.ClientHelper;

/**
 * @author ljing12 This class requires to complete the methods
 */
public class Student extends User implements Serializable {
	// int studentNumber;
	String studentNumber = "000";
	String password;
	String studentName = "unknown";
	boolean isGraduate = true;
	int grade = 1;
	ArrayList scores = new ArrayList();
	ArrayList<Course> ownCourseList = new ArrayList<Course>();
	final Identification identity = Identification.STUDENT;
	ClientHelper cHelper = new ClientHelper();

	public Student() {

	}

	public Student(String studentNumber) {
		this.studentNumber = studentNumber;
		this.setNameFromFile();
	}

	public Student(String studnentNum, String password,
			boolean isRegisterOrLogin) {
		this.studentNumber = studnentNum;
		this.password = password;
	}

	public Student(String studentNumber, String studnentName) {
		this.studentNumber = studentNumber;
		this.studentName = studentName;
	}

	public void setNameFromFile() {
		try {
			String[] str = IOHelper.findOneRecord(0, "studentsList.txt",
					this.studentNumber).split(" ");

			this.studentName = str[1];
		} catch (Exception e) {
			System.out.println("error in synchro the data");
		}
	}

	public void startOperation() {
		final int EXIT = 0;
		final int SHOWLIST = 1;
		final int SELECT = 2;
		final int QUIT = 3;
		final int SHOWSCORE = 4;
		boolean endOperation = false;
		System.out
				.println("Hi .students!\n Welcome to the platform>The things you can do is listed following");
		outter: while (!endOperation) {
			String operationString = "(1)View all your courses|format:Show mycourseList\n"
					+ "(2)select your lesson|format:Select couese [courseNumber]\n"
					+ "(3)quit a coursen|format:Quit course [courseNumber]\n"
					+ "(4)View all your scores|format:Show score [courseNumber\n"
					+ "(5)Exit";
			System.out.println(operationString);

			String cmdString = (IOHelper.readConsoleNextLine());
			String[] strings = cmdString.split(" ");
			int cmdInt = CommandHelper.getStudentCmd(cmdString);
			switch (cmdInt) {
			case 0:
				break outter;
			case 1:
				showMyList();
				break;
			case 2:
				select(strings[2].substring(1, strings[2].indexOf("]")));
				break;
			case 3:
				quit(strings[2].substring(1, strings[2].indexOf("]")));
				break;
			case 4:
				showScore(strings[2].substring(1, strings[2].indexOf("]")));
				break;
			default:
				break;
			}
			System.out
					.println("Do you wnat to exit? If so input 'Yes' ,or 'No'");
			if (IOHelper.readConsoleNextLine().equals("Yes")) {
				endOperation = true;
			}
		}

	}

	public void showMyList() {
		StringBuffer toShow = new StringBuffer();
		String allCourseString = IOHelper.findAllMatchInfo(1,
				"courseSelected.txt", this.studentNumber);
		if (IOHelper.ifExisted(1, "courseSelected.txt", this.studentNumber)) {
			if (allCourseString.contains("\n")) {
				String[] allCourse = allCourseString.split("\n");

				for (int i = 0; i < allCourse.length; i++) {
					allCourse[i] = allCourse[i].substring(0,
							allCourse[i].indexOf(" "));
					allCourse[i] = allCourse[i].trim();
					// System.out.print(allCourse[i]);
					toShow.append(IOHelper.findOneRecord(0, "courseList.txt",
							allCourse[i]));
					toShow.append("\n");
				}
				String toShowString = toShow.toString().trim();
				System.out.println(toShowString);
			} else {
				allCourseString = allCourseString.substring(0,
						allCourseString.indexOf(" "));
				allCourseString = allCourseString.trim();
				toShow.append(IOHelper.findOneRecord(0, "courseList.txt",
						allCourseString));
				System.out.println(toShow.toString());

			}
		} else {
			System.out.println("Sorry you haven't choose any course now");
			return;
		}
	}

	public void select(String courseNum) {
		String recordString = null;
		if (IOHelper.ifExisted(0, "courseList.txt", courseNum)) {
			Course c = new Course(IOHelper.findOneRecord(0, "courseList.txt",
					courseNum));
			if (c.getProperity() != 1) {
				// only allow student to select the optional course
				if (!this.isLearnCourse(courseNum)) {
					recordString = courseNum + " " + this.getNum();
					IOHelper.addOneLine("courseSelected.txt", recordString);
				} else {
					System.out.println("You have choosen the course");
				}
			} else {
				System.out
						.println("Sorry compulosory course must be appointed by the administer!");
			}
		} else {
			System.out.println("sorry the course don't exist");
		}

	}

	public void quit(String courseNum) {
		String recordString = null;
		if (IOHelper.ifExisted(0, "courseList.txt", courseNum)) {
			if (this.isLearnCourse(courseNum)) {
				Course course = new Course(IOHelper.findOneRecord(0,
						"courseList.txt", courseNum));
				if (course.getProperity() != 1) {
					IOHelper.deleteOneLine("courseSelected.txt", 0, courseNum,
							1, this.getNum());
				} else {
					System.out
							.println("sorry you can not quit a compulsory course");
				}
			} else {
				System.out.println("You have not choosen the course yet");
			}
		} else {
			System.out.println("sorry the course don't exist");
		}

	}

	public void showScore(String courseNum) {

		if (isLearnCourse(courseNum)) {
			System.out.println("Your score of the course :"
					+ courseNum
					+ " is "
					+ IOHelper.getOneTerm(0, 1, "Scores.txt", this.getNum(),
							courseNum, 2));
		} else {
			System.out.println("sorry you don't learn the course");
		}
	}

	/**
	 * To make sure that a student has chosen a course
	 * 
	 * @param courseNum
	 * @return
	 */
	public boolean isLearnCourse(String courseNum) {

		boolean isLearn = false;
		String allCourseString = IOHelper.findAllMatchInfo(1,
				"courseSelected.txt", this.studentNumber);
		if (allCourseString.contains("\n")) {
			String[] allCourse = allCourseString.split("\n");
			for (int i = 0; i < allCourse.length; i++) {
				allCourse[i] = allCourse[i].substring(0,
						allCourse[i].indexOf(" "));
				allCourse[i] = allCourse[i].trim();
				if (allCourse[i].equals(courseNum)) {
					return true;
				}
			}

		} else {
			allCourseString = allCourseString.substring(0,
					allCourseString.indexOf(" ")).trim();
			if (allCourseString.equals(courseNum)) {
				return true;
			}
		}
		return isLearn;
	}

	public String toString() {
		return studentNumber + " " + studentName
				+ ((isGraduate) ? "graduate" : "underGraduate") + " " + grade; // override
	}

	public String getNum() {
		return this.studentNumber;
	}

	public String getName() {
		return this.studentName;
	}

	@Override
	public String getPassword() {
		// TODO Auto-generated method stub
		return this.password;
	}

	public void getOwnCourseList() {
		for (int i = 0; i < ownCourseList.size(); i++) {
			System.out.println(ownCourseList.get(i));
		}
	}

	public void setScore(String valueAt) {
		// TODO Auto-generated method stub

	}

	public String processNetGetCourseInfo(String courseNumString) {
		// TODO Auto-generated method stub
		String returnString = "";
		String cmdString = "StudentOp!1!" + courseNumString;
		returnString = cHelper.sendToNet(cmdString).trim();
		if (returnString.equals("Fail")) {
			return "";
		}
		return returnString;
	}

	public boolean processNetHasRight(String courseNum, String studentNumString) {
		// TODO Auto-generated method stub
		boolean result = false;
		String cmdString = "StudentOp!2!" + courseNum + "!" + studentNumString;
		String resultString = cHelper.sendToNet(cmdString);
		if (resultString.equals("Fail")) {
			result = false;
		} else {
			result = true;
		}
		return result;
	}

	public String processNetGetScore(String courseNum, String studentNumString) {
		// TODO Auto-generated method stub
		String cmdString = "StudentOp!3!" + courseNum + "!" + studentNumString;
		String resultString = cHelper.sendToNet(cmdString);
		if (resultString.equals("Fail")) {
			return null;
		} else {
			resultString = resultString.split(" ")[2];
		}
		return resultString;
	}

	public void processNetSelectC(String courseNum, String studentNumString) {
		// TODO Auto-generated method stub
		String cmdString = "StudentOp!4!" + courseNum + "!" + studentNumString;
		cHelper.sendToNet(cmdString);

	}

	public void proceessNetQuit(String courseNum, String studentNumString) {
		// TODO Auto-generated method stub
		String cmdString = "StudentOp!5!" + courseNum + "!" + studentNumString;
		cHelper.sendToNet(cmdString);

	}

	public String[] processNetShowAllCourse(String studentNumber2) {
		// TODO Auto-generated method stub
		String[] allcourseInfoStrings = null;
		String cmdString = "StudentOp!6!" + studentNumber2;
		String resultString = cHelper.sendToNet(cmdString);
		if (resultString.endsWith("Fail")) {
			return null;
		} else {
			if (resultString.contains("!")) {
				allcourseInfoStrings = resultString.split("!");
			} else {
				resultString += "\n";
				allcourseInfoStrings = resultString.split("\n");
			}
		}
		return allcourseInfoStrings;
	}

	public String processNetGetNameFromFile(String studentNumber) {
		// TODO Auto-generated method stub
		String cmdString = "StudentOp!7!" + studentNumber;
		String resultString = cHelper.sendToNet(cmdString);
		if (resultString.equals("Fail")) {
			return null;
		} else {
			return resultString;
		}
	}

}
